package xyz.labmem.main.modular.common.controller

import cn.hutool.core.bean.BeanUtil
import xyz.labmem.base.sys.base.BaseController
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import xyz.labmem.base.core.result.RV
import xyz.labmem.base.sys.consts.PathPrefixConst
import xyz.labmem.main.modular.common.controller.response.DictDataRes
import xyz.labmem.main.modular.common.controller.response.DictTypeRes
import xyz.labmem.base.sys.module.dict.service.SysDictService

/**
 * @Author lty
 * @Date 2021/12/20 14:37
 */
@RestController
@Api(tags = ["字典获取"])
@RequestMapping("${PathPrefixConst.COMMON_PATH_PREFIX}dict")
class ComSysDictController(
    private var sysDictService: SysDictService,
) : BaseController() {

    @GetMapping("getTypeByCode/{code}")
    @ApiOperation(value = "通过code获取字典类型")
    fun getTypeByCode(
        @ApiParam(value = "code", required = true) @PathVariable code: String
    ): RV<DictTypeRes> {
        val res = DictTypeRes()
        BeanUtil.copyProperties(sysDictService.getTypeByCode(code), res)
        return success(res)
    }

    @GetMapping("getDateByTypeCode/{code}")
    @ApiOperation(value = "通过类型code获取字典值")
    fun getDateByTypeCode(
        @ApiParam(value = "code", required = true) @PathVariable code: String
    ): RV<List<DictDataRes>> {
        val res = ArrayList<DictDataRes>()
        sysDictService.getDataListByTypeCode(code).forEach {
            res.add(DictDataRes().apply {
                value = it.value
                this.code = it.code
            })
        }
        return success(res)
    }

}